//
//  MKTChatMsgLinksListTVCellOther.m
//  MKUI
//
//  Created by 苏尚进 on 2021/7/15.
//Copyright © 2021 moon. All rights reserved.
//

#import "MKTChatMsgLinksListTVCellOther.h"

@interface MKUITabBarItemViewContainerCollectionView_MKTChatMsgLinksListTVCellOther : MKUITabBarItemViewContainerCollectionView
@property(nonatomic,strong) UIView *lineView;
@end
@implementation MKUITabBarItemViewContainerCollectionView_MKTChatMsgLinksListTVCellOther
- (id)initWithFrame:(CGRect)frame{
    if (self=[super initWithFrame:frame]) {
        self.lineView = [[UIView alloc] init];
        self.lineView.backgroundColor = [UIColor whiteColor];
        [self addSubview:self.lineView];
    }
    return self;
}
- (void)layoutSubviews{
    [super layoutSubviews];
    CGRect bounds = self.bounds;
    CGRect f1 = bounds;
    f1.size.height = 1.0/[UIScreen mainScreen].scale;
    MKCGRectAlignMaxYToRect(&f1, bounds);
    self.lineView.frame = f1;
}
- (void)colorTraitCollectionDidChange{
    self.collectionView.backgroundColor = [UIColor clearColor];
}
@end

@interface MKUITabBarItemCollectionViewCell_MKTChatMsgLinksListTVCellOther : MKUITabBarItemCollectionViewCell
@end
@implementation MKUITabBarItemCollectionViewCell_MKTChatMsgLinksListTVCellOther
- (id)initWithFrame:(CGRect)frame{
    if (self=[super initWithFrame:frame]) {
        self.tabBarItemButton.titleLabel.font = [UIFont systemFontOfSize:14];
    }
    return self;
}
- (void)colorTraitCollectionDidChange{
    [self.tabBarItemButton setTitleColor:[UIColor lightTextColor] forState:(UIControlStateNormal)];
    [self.tabBarItemButton setTitleColor:[UIColor whiteColor] forState:(UIControlStateSelected)];
}
@end

@interface MKTChatLinkTVCell_MsgLinksList : MKUITableViewCellBase
@property(nonatomic,strong) UILabel *titleLabel;
@property(nonatomic,strong) UIView *lineView;
@property(nonatomic,strong) MKUIFlowLayoutConstraint *flowlayout;
@end
@implementation MKTChatLinkTVCell_MsgLinksList
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self=[super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.backgroundColor = [UIColor clearColor];
        self.titleLabel = [[UILabel alloc] init];
        self.titleLabel.numberOfLines = 0;
        self.titleLabel.textColor = [UIColor whiteColor];
        self.titleLabel.font = [UIFont systemFontOfSize:14];
        [self.contentView addSubview:self.titleLabel];
        //
        self.lineView = [[UIView alloc] init];
        self.lineView.backgroundColor = [UIColor whiteColor];
        [self addSubview:self.lineView];
        //
        self.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        //
        self.flowlayout = [[MKUIFlowLayoutConstraint alloc] initWithItems:@[self.titleLabel] constraintParam:(MKUIFlowLayoutConstraintParam_H_C_L) contentInsets:UIEdgeInsetsMake(5, 0, 5, 0) interitemSpacing:5];
    }
    return self;
}
- (void)customLayoutSubviews{
    [super customLayoutSubviews];
    CGRect bounds = self.contentView.bounds;
    self.flowlayout.bounds = bounds;
    [self.flowlayout layoutItemsWithResizeItems:YES];
    //
    CGRect f1 = self.bounds;
    f1.size.width -= 10;
    f1.size.height = 1.0/[UIScreen mainScreen].scale;
    MKCGRectAlignMaxYToRect(&f1, self.bounds);
    MKCGRectAlignMidXToRect(&f1, self.bounds);
    self.lineView.frame = f1;
}
- (CGSize)customSizeThatFits:(CGSize)size{
    CGSize s = [self.flowlayout sizeThatFits:size resizeItems:YES];
    s.height = MAX(s.height,30);
    return s;
}
- (void)customReloadCellModel{
    [super customReloadCellModel];
    MKTChatLink *modelValue = self.cellModel.modelValue;
    self.titleLabel.text = modelValue.title;
    self.lineView.hidden = self.cellModel.isLastInAllCellModels;
}
@end

@interface MKTChatLinksTabView : MKUITabBarContentView
@property(nonatomic,assign) NSInteger pagenum;//默认为3
@property(nonatomic,assign) NSInteger pageindex;
@property(nonatomic,strong) MKTChatLinksTab *tab;
@property(nonatomic,strong) MKUITableView *tableView;
- (void)reloadData;
@end

@implementation MKTChatLinksTabView
- (id)initWithFrame:(CGRect)frame{
    if (self=[super initWithFrame:frame]) {
        self.tableView = [[MKUITableView alloc] initWithFrame:self.bounds style:(UITableViewStylePlain)];
        self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        self.tableView.backgroundColor = [UIColor clearColor];
        [self addSubview:self.tableView];
    }
    return self;
}
- (void)layoutSubviews{
    [super layoutSubviews];
    CGRect bounds = self.bounds;
    self.tableView.frame = bounds;
}
- (CGSize)sizeThatFits:(CGSize)size{
    CGSize s = size;
    s.height = [self.tableView mk_heightThatFits:size.width];
    return s;
}
- (void)tabBarContentViewWillAppear:(BOOL)animated{
    [self reloadData];
}
- (void)setTab:(MKTChatLinksTab *)tab{
    _tab = tab;
    self.tabBarItem.title = self.tab.title;
}
- (void)reloadData{
    [self.tableView.model removeAllSectionModels];
    NSInteger pageindex = self.pageindex;
    NSInteger pagenum = self.pagenum;
    for (int i=0; i<pagenum; i++) {
        NSInteger index = pageindex*pagenum+i;
        if(index>=self.tab.links.count){
            break;
        }
        MKTChatLink *link = self.tab.links[index];
        MKUITableViewCellModel *cm = [MKUITableViewCellModel modelWithValue:link cellClass:[MKTChatLinkTVCell_MsgLinksList class] whenClick:^(__kindof MKUITableViewCellModel * _Nonnull cellModel) {
            [cellModel.tableView deselectRowAtIndexPath:cellModel.indexPathInModel animated:YES];
            [link handleClick:cellModel.tableViewCell];
        }];
        [self.tableView.model addCellModel:cm];
    }
    [self.tableView.model reloadTableViewData];
}
@end

#import "MKTChatBubbleView.h"
@interface MKTChatMsgLinksListTVCellOther ()<MKUITabBarControllerDelegate>
@property(nonatomic,strong) UILabel *titleLabel;
@property(nonatomic,strong) MKTChatBubbleView *bubbleView;
@property(nonatomic,strong) MKUIFlowLayoutButton *refreshButton;
@property(nonatomic,strong) MKUITabBarController *tabController;
@property(nonatomic,strong) MKUIFlowLayoutConstraint *flowlayout;
@property(nonatomic,strong) NSMutableArray<MKTChatLinksTabView *> *tabViewsCache;
@end

@implementation MKTChatMsgLinksListTVCellOther
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
	if (self=[super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        //
        self.tabViewsCache = [[NSMutableArray alloc] init];
        //
        self.titleLabel = [[UILabel alloc] init];
        self.titleLabel.textAlignment = NSTextAlignmentLeft;
        self.titleLabel.font = [UIFont systemFontOfSize:14];
        self.titleLabel.textColor = [UIColor grayColor];
        [self.contentView addSubview:self.titleLabel];
        //
        self.bubbleView = [[MKTChatBubbleView alloc] init];
        self.bubbleView.bgView.backgroundColor = [UIColor grayColor];
        [self.contentView addSubview:self.bubbleView];
        //
        self.tabController = [[MKUITabBarController alloc] initWithItemContainerClass:[MKUITabBarItemViewContainerCollectionView_MKTChatMsgLinksListTVCellOther class] contentContainerClass:[MKUITabBarContentContainerOneTabView class]];
        self.tabController.delegate = self;
        self.tabController.itemCollectionContainer.itemCollectionCellClass = [MKUITabBarItemCollectionViewCell_MKTChatMsgLinksListTVCellOther class];
        self.tabController.itemCollectionContainer.collectionView.backgroundColor = [UIColor clearColor];
        self.tabController.itemCollectionContainer.itemIndicatorViewClass = [MKUITabBarItemIndicatorView class];
        [self.bubbleView addSubview:self.tabController.itemContainer];
        [self.bubbleView addSubview:self.tabController.contentContainer];
        self.bubbleView.containerLayout = [[MKUIFlowLayoutConstraint alloc] initWithItems:@[self.tabController.itemContainer,self.tabController.contentContainer] constraintParam:(MKUIFlowLayoutConstraintParam_V_T_L) contentInsets:[MKTChatBubbleView defaultContentInsets] interitemSpacing:0];
        //
        self.refreshButton = [[MKUIFlowLayoutButton alloc] initWithContentStyle:(MKUIFlowLayoutButtonContentStyleHorizontal)];
        self.refreshButton.interitemSpacing = 5;
        [self.refreshButton setTitle:@"换一批" forState:(UIControlStateNormal)];
        self.refreshButton.contentInsets = UIEdgeInsetsMake(5, 0, 5, 0);
        self.refreshButton.imageSize = CGSizeMake(16, 16);
        [self.refreshButton setImage:[UIImage imageNamed:@"app-icon-11.png"] forState:(UIControlStateNormal)];
        self.refreshButton.titleLabel.font = [UIFont systemFontOfSize:14];
        [self.refreshButton setTitleColor:[UIColor whiteColor] forState:(UIControlStateNormal)];
        [self.refreshButton mk_addClickActionBlock:^(MKTChatMsgLinksListTVCellOther *self) {
            MKTChatMsgLinksList *modelValue = self.cellModel.modelValue;
            NSInteger selectedIndex = [self.cellModel mk_integerForKeyPath:@"selectedIndex"];
            MKTChatLinksTab *tab = modelValue.linkslist[selectedIndex];
            NSInteger pageindex = [self.cellModel mk_integerForKeyPath:[NSString stringWithFormat:@"pageindex_%@",@(selectedIndex)]];
            NSInteger pagenum = modelValue.pagenum;
            NSInteger pagecount = ceil(1.0*tab.links.count/pagenum);
            pageindex = (pageindex+1)%pagecount;
            self.cellModel[[NSString stringWithFormat:@"pageindex_%@",@(selectedIndex)]] = @(pageindex);
            [self.cellModel refresh];
        } context:self];
        [self.bubbleView addSubview:self.refreshButton];
        //
        self.bubbleView.containerLayout = [[MKUIFlowLayoutConstraint alloc] initWithItems:@[self.tabController.itemContainer,self.tabController.contentContainer,self.refreshButton] constraintParam:(MKUIFlowLayoutConstraintParam_V_T_L) contentInsets:[MKTChatBubbleView defaultContentInsets] interitemSpacing:5];
        //
        self.flowlayout = [[MKUIFlowLayoutConstraint alloc] initWithItems:@[self.titleLabel,self.bubbleView] constraintParam:(MKUIFlowLayoutConstraintParam_V_T_L) contentInsets:UIEdgeInsetsMake(5, 10, 20, 10) interitemSpacing:5];
	}
	return self;
}
- (void)customLayoutSubviews{
	[super customLayoutSubviews];
	CGRect bounds = self.contentView.bounds;
    self.flowlayout.bounds = bounds;
    [self.flowlayout layoutItemsWithResizeItems:YES];
}
- (CGSize)customSizeThatFits:(CGSize)size{
    return [self.flowlayout sizeThatFits:size resizeItems:YES];
}
- (void)customReloadCellModel{
    [super customReloadCellModel];
	MKTChatMsgLinksList *modelValue = self.cellModel.modelValue;
    self.titleLabel.text = modelValue.title;
    NSInteger selectedIndex = [self.cellModel mk_integerForKeyPath:@"selectedIndex"];
    NSMutableArray<MKTChatLinksTabView *> *tabContents = [[NSMutableArray alloc] initWithCapacity:modelValue.linkslist.count];
    for (int i=0;i<modelValue.linkslist.count;i++) {
        MKTChatLinksTab *tab = modelValue.linkslist[i];
        MKTChatLinksTabView *tabView = nil;
        if(self.tabViewsCache.count>i){
            tabView = self.tabViewsCache[i];
        }else{
            tabView = [[MKTChatLinksTabView alloc] init];
            [self.tabViewsCache addObject:tabView];
        }
        tabView.tab = tab;
        tabView.pagenum = modelValue.pagenum;
        tabView.pageindex = [self.cellModel mk_integerForKeyPath:[NSString stringWithFormat:@"pageindex_%@",@(selectedIndex)]];
        [tabContents addObject:tabView];
    }
    self.tabController.contents = tabContents;
    self.tabController.selectedIndex = selectedIndex;
    [self.tabController reloadData];
}
#pragma mark - delegate:MKUITabBarControllerDelegate
- (void)tabBarController:(MKUITabBarController *)controller didSelectIndex:(NSInteger)index{
    NSInteger selectedIndex = [self.cellModel mk_integerForKeyPath:@"selectedIndex"];
    if(selectedIndex!=index){
        self.cellModel[@"selectedIndex"] = @(index);
        [self.cellModel refresh];
    }
}
@end
